package Solutions

func findAllRecipes(recipes []string, ingredients [][]string, supplies []string) []string {
	var suppliesMp = make(map[string]bool, len(supplies))
	var recipesMp = make(map[string]int, len(recipes))
	var chain = make(map[string]bool)
	var f func(int) bool
	f = func(i int) bool {
		if suppliesMp[recipes[i]] {
			return true
		}
		chain[recipes[i]] = true
		defer delete(chain, recipes[i])
		for ind := 0; ind < len(ingredients[i]); ind++ {
			if chain[ingredients[i][ind]] {
				return false
			}
			if !suppliesMp[ingredients[i][ind]] {
				if v, ok := recipesMp[ingredients[i][ind]]; ok {
					if !f(v) {
						return false
					}
				} else {
					return false
				}
			}
		}
		suppliesMp[recipes[i]] = true
		return true
	}
	for i, v := range recipes {
		recipesMp[v] = i
	}
	for _, v := range supplies {
		suppliesMp[v] = true
	}
	var ans = make([]string, 0, len(recipes))
	for i, v := range recipes {
		if f(i) {
			ans = append(ans, v)
		}
	}
	return ans

}